$(function() {
//	$("#top_menu").load("../../../../uiframe/Top.html", function(){
//		$("#xitongshezhi").addClass("active");
//	}); 
	
	// 加载数据列表
	getList();
	//绑定下拉框change事件，当下来框改变时调用 getList()方法
    $(document).ready(function() {
        $("#isallocate").change(function() { getList(); }); 
        });
	
	//初始化dategrid   可定制高宽，标题
	$('#tt').datagrid({   
	    url:null,   
	    pagination:true,   
	    pageSize:_pageSize,   
	    pageNumber:1,   
	    rownumbers:true,
	    singleSelect:false,
	    title:'业务管理 > 医患关系管理 ',
    	width:$("#body").width(),
    	height:_height,
    	toolbar:"#toolbar"
	});   
	
	//翻页设置
	$('#tt').datagrid('getPager').pagination({   
	    displayMsg:'当前显示从 [{from}] 到 [{to}] 共[{total}]条记录',   
	    onSelectPage : function(pPageIndex, pPageSize) {   
	        //改变opts.pageNumber和opts.pageSize的参数值，用于下次查询传给数据层查询指定页码的数据   
	        var gridOpts = $('#tt').datagrid('options');   
	        gridOpts.pageNumber = pPageIndex;   
	        gridOpts.pageSize = pPageSize;     
	        //异步获取数据到javascript对象，入参为查询条件和页码信息   
	        //使用loadDate方法加载Dao层返回的数据   
	        $('#tt').datagrid('loadData',{"total" : bannerArray.length,"rows" : getPageList(bannerArray,pPageIndex,pPageSize)});   
	    }   
	});
	
});


//缓存用户列表
var bannerArray;
var curpage=1;
//获取列表数据
function getList(){
	var isallocate =$("#isallocate").val();
	var urlStr = serverPath + "/server/doctor/diabetes/doctorPatient.do";
	var jsonStr = 
	    '{'+
	    '	   "requestid":"",'+
	    '	   "requestsource":"diabetes",'+
	    '	   "sessionid":"'+getCookie('sessionId')+'",'+
	    '	   "uid":"'+getCookie('Uid')+'",'+
	    '	   "method":"getAllList",'+
	    '	   "version":"1.0",'+
	    '	   "requestlist":'+
	    '	   [{'+
	    '           "flag":"' + isallocate + '"'+
	    '	   }]'+
	    '	}';
	UseTKAjax(urlStr, jsonStr, document.cookie, task, '1');
}

//处理用户列表
function task(nodeArray) {
	if (nodeArray && nodeArray.resultlist.length > 0) {
		//排序
		bannerArray=nodeArray.resultlist[0].resultlist;
		$('#tt').datagrid('loadData',{
			"total": bannerArray.length,
			"rows":getPageList(bannerArray,1,_pageSize)
		});
	} 
}

//提交新增结果
function submform() {
	$('#fm').submit();
}

//查询所有健康管理师信息列表
function getHealthManagerList(){
	var row = $("#tt").datagrid("getSelections");
	if(row.length > 0){
		var urlStr = serverPath + "/server/doctor/diabetes/doctorPatient.do";
		var jsonStr = 
		    '{'+
		    '	   "requestid":"",'+
		    '	   "requestsource":"diabetes",'+
		    '	   "sessionid":"'+getCookie('sessionId')+'",'+
		    '	   "uid":"'+getCookie('Uid')+'",'+
		    '	   "method":"getHealthManagerList",'+
		    '	   "version":"1.0",'+
		    '	   "requestlist":'+
		    '	   ['+
		                '{}'+
		    '	   ]'+
		    '	}';
		UseTKAjax(urlStr, jsonStr, document.cookie, showHealthManagerInfo, '1');
	}else{
		alert('请选择所要分配的人！');
	}
}

function showHealthManagerInfo(nodeArray){
    $("#dlg").dialog("open").dialog('setTitle', '健康管理师信息');
	if (nodeArray && nodeArray.resultlist.length > 0) {
		//排序
		bannerArray=nodeArray.resultlist[0].resultlist;
		$('#tt1').datagrid('loadData',{
			"total": bannerArray.length,
			"rows":getPageList(bannerArray,1,_pageSize)
		});
	} 
}
function submform(){
	var users = $("#tt").datagrid("getSelections");
	var healthManager = $("#tt1").datagrid("getSelected");
	if(healthManager != null){
		var userids = "";
		for(var i in users){
			userids = users[i].user_id + ":";
		}
//		for(var i = 0;i<users.length;i++){
//			userids = users[i].user_id + ":";
//		}
		userids = userids.substring(0, userids.length-1);
		var urlStr = serverPath + "/server/doctor/diabetes/doctorPatient.do";
		var jsonStr = 
		    '{'+
		    '	   "requestid":"",'+
		    '	   "requestsource":"diabetes",'+
		    '	   "sessionid":"'+getCookie('sessionId')+'",'+
		    '	   "uid":"'+getCookie('Uid')+'",'+
		    '	   "method":"allocation",'+
		    '	   "version":"1.0",'+
		    '	   "requestlist":'+
		    '	   [{'+
		    '           "userids":"' + userids + '",'+
		    '           "hmid":"' + healthManager.user_id + '"'+
		    '	  }]'+
		    '	}';
		UseTKAjax(urlStr, jsonStr, document.cookie, reloadData, '1');
	}else{
		alert("请选择健康管理师！");
	}
	$("#fm").form("clear");
	$("#dlg").dialog("close");
}

function cancle(){
	$("#fm").form("clear");
	$('#dlg').dialog('close');
}

function reloadData(narrary){
	if(narrary.resultcode == '0'){
		alert("操作成功！");
	}else{
		alert('操作失败！');
	}
	getList();
}
